package com.google.code.mukiranaproject.netlib;

import java.net.*;
import java.io.*;

public class URLHelper {
	
	/*
	 * VARIABLES
	 */
	private URL url;
	
	private BufferedReader in;
	
	/*
	 * CONSTRUCTOR
	 */
	
	/*
	 * URLHelper Constructor
	 * url -- Endereço em formato de texto da página desejada
	 */
	public URLHelper(String url) throws MalformedURLException, IOException {
		this.url = new URL(url);
		this.open();
	}

	/*
	 * PUBLIC METHODS
	 */
	
	/*
	 * Estabelece uma conexão e preenche o BufferedReader
	 */
	private void open() throws IOException {
		this.in = new BufferedReader(
				new InputStreamReader(this.url.openStream()));
	}
	
	/*
	 * Exibe o conteúdo da URL passada.
	 */
	public void printContent() throws IOException {
		String inputLine;
	
		while ((inputLine = this.in.readLine()) != null) {
			System.out.println(inputLine);
		}
	}
	
	/*
	 * Transforma conteudo em uma Stringona.
	 */
	public String contentToString() throws IOException {
		String stringona = "";
		String inputLine;
		
		while ((inputLine = this.in.readLine()) != null) {
			stringona += inputLine + "\n";
		}
		
		return stringona;
	}
	
	/*
	 * Fecha a conexão 
	 */
	public void close() throws IOException {
		in.close();
	}

	
	/*
	 * GETTERS/SETTERS
	 */
	public URL getUrl() {
		return this.url;
	}
	
} 